<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"
    />
    <title>单例模式-透明-代理</title>
</head>

<body>
    <h2>代理</h2>
    <p>1. 将CreateDiv变为普通类，通过与代理类组合实现单例。同时遇到需要多个实例的情况也不再需要改变CreateDiv函数啦！！</p>
    <p>2. 同时遇到后期变通需要多个实例的情况也不再需要改变CreateDiv函数啦！！</p>

    <script type="text/javascript">

        var CreateDiv = function (html) {
            this.html = html
            this.init()
        }
        CreateDiv.prototype.init = function () {
            var div = document.createElement('div')
            div.innerHTML = this.html
            document.body.appendChild(div)
        }
        var ProxySingletonCreateDiv = (function () {
            var instance
            return function (html) {
                if (!instance) {
                    instance = new CreateDiv(html)
                }
                return instance
            }
        })()

        var a = new ProxySingletonCreateDiv('sven-a')
        var b = new ProxySingletonCreateDiv('sven2')
        var c = new ProxySingletonCreateDiv('sven3')
        console.log(a === b) // true 单例

        var aa = new CreateDiv('sven-aa')
        var bb = new CreateDiv('sven-bb')
        var cc = new CreateDiv('sven-cc')
        console.log(aa === bb) // false ，可以创建多个
    </script>

</body>

</html>